home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / bash-1.12 / dist / endian.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-06  |  2.2 KB  |  90 lines

  1. /* endian.c -- A trick for determining the byte order of a machine. */
  2. #include <stdio.h>
  3.  
  4. /* The name of this program, as taken from argv[0]. */
  5. char *progname;
  6.  
  7. /* The name of the source file that this code is made from. */
  8. char source_name[256];
  9.  
  10. /* The name of the define.  Either "BIG_ENDIAN" or "LITTLE_ENDIAN". */
  11. char *endian_define;
  12.  
  13. /* Stuffed value of "ABCD" in a long, followed by a 0. */
  14. long int str[2] = { 0x41424344, 0x0 };
  15.  
  16. /* Stuff "ABCD" into a long, and compare it against a character string
  17.    "ABCD".  If the results are EQ, the machine is big endian like a 68000
  18.    or Sparc, otherwise it is little endian, like a Vax, or 386. */
  19. main (argc, argv)
  20.      int argc;
  21.      char **argv;
  22. {
  23.   register int i;
  24.   FILE *stream = (FILE *)NULL;
  25.   char *stream_name = "stdout";
  26.  
  27.   progname = argv[0];
  28.  
  29.   for (i = strlen (progname); i > 0; i--)
  30.     if (progname[i] == '/')
  31.       {
  32.     progname = progname + i + 1;
  33.     break;
  34.       }
  35.  
  36.   strcpy (source_name, progname);
  37.   for (i = strlen (source_name); i > 0; i--)
  38.     if (source_name[i] == '.')
  39.       {
  40.     source_name[i] = '\0';
  41.     break;
  42.       }
  43.  
  44.   strcat (source_name, ".c");
  45.  
  46.   if (argc == 1)
  47.     {
  48.       stream_name = "stdout";
  49.       stream = stdout;
  50.     }
  51.   else if (argc == 2)
  52.     {
  53.       stream_name = argv[1];
  54.       stream = fopen (stream_name, "w");
  55.     }
  56.   else
  57.     {
  58.       fprintf (stderr, "Usage: %s [output-file]\n", progname);
  59.       exit (1);
  60.     }
  61.  
  62.   if (!stream)
  63.     {
  64.       fprintf (stderr, "%s: %s Cannot be opened or written to.\n",
  65.            progname, stream_name);
  66.       exit (2);
  67.     }
  68.  
  69.   if (strcmp ((char *)&str[0], "ABCD") == 0)
  70.     endian_define = "BIG_ENDIAN";
  71.   else
  72.     endian_define = "LITTLE_ENDIAN";
  73.  
  74.   fprintf (stream, "/* %s - Define BIG or LITTLE endian. */\n\n", stream_name);
  75.   fprintf (stream,
  76. "/* This file was automatically created by `%s'.  You shouldn't\n\
  77.    edit this file, because your changes will be overwritten.  Instead,\n\
  78.    edit the source code file `%s'. */\n\n",
  79.        progname, source_name);
  80.  
  81.   fprintf (stream, "#if !defined (%s)\n", endian_define);
  82.   fprintf (stream, "#  define %s\n", endian_define);
  83.   fprintf (stream, "#endif /* %s */\n", endian_define);
  84.  
  85.   if (stream != stdout)
  86.     fclose (stream);
  87.  
  88.   exit (0);
  89. }
  90.